package com.yuanqiworld.vw.web;

import com.yuanqiworld.vw.domain.RoleInfo;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/roleinfoes")
@Controller
@RooWebScaffold(path = "roleinfoes", formBackingObject = RoleInfo.class)
@RooWebJson(jsonObject = RoleInfo.class)
public class RoleInfoController {

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid RoleInfo roleInfo, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (RoleInfo.findRoleInfoesByRoleName(roleInfo.getRoleName()).getResultList().size() > 0) {
            bindingResult.rejectValue("roleName", "rolename.invalid");
        }
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, roleInfo);
            return "roleinfoes/create";
        }
        uiModel.asMap().clear();
        roleInfo.persist();
        return "redirect:/roleinfoes/" + encodeUrlPathSegment(roleInfo.getId().toString(), httpServletRequest);
    }

    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
    public String update(@Valid RoleInfo roleInfo, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, roleInfo);
            return "roleinfoes/update";
        }
        uiModel.asMap().clear();
        roleInfo.merge();
        return "redirect:/roleinfoes/" + encodeUrlPathSegment(roleInfo.getId().toString(), httpServletRequest);
    }
}
